using System;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class GridPanel : Panel
	{
		private const int defaultCellRow = 4;
		private const int defaultCellCol = 7;
		private double CellW;
		private double CellH;
		public static DependencyProperty CellRowProperty = DependencyProperty.Register("CellRow", typeof(int), typeof(GridPanel), new PropertyMetadata(4, new PropertyChangedCallback(GridPanel.OnItemPropertyChanged)));
		public static DependencyProperty CellColProperty = DependencyProperty.Register("CellCol", typeof(int), typeof(GridPanel), new PropertyMetadata(7, new PropertyChangedCallback(GridPanel.OnItemPropertyChanged)));
		public int CellRow
		{
			get
			{
				return (int)base.GetValue(GridPanel.CellRowProperty);
			}
			set
			{
				base.SetValue(GridPanel.CellRowProperty, value);
			}
		}
		public int CellCol
		{
			get
			{
				return (int)base.GetValue(GridPanel.CellColProperty);
			}
			set
			{
				base.SetValue(GridPanel.CellColProperty, value);
			}
		}
		protected override Size MeasureOverride(Size availableSize)
		{
			Size result = base.MeasureOverride(availableSize);
			if (!double.IsInfinity(availableSize.get_Width()) && !double.IsInfinity(availableSize.get_Height()))
			{
				result = availableSize;
			}
			this.CellW = result.get_Width() / (double)this.CellCol;
			this.CellH = result.get_Height() / (double)this.CellRow;
			for (int i = 0; i < base.get_Children().get_Count(); i++)
			{
				base.get_Children().get_Item(i).Measure(new Size(this.CellW, this.CellH));
			}
			return result;
		}
		protected override Size ArrangeOverride(Size finalSize)
		{
			int num = this.CellRow * this.CellCol;
			for (int i = 0; i < base.get_Children().get_Count(); i++)
			{
				if (i < num)
				{
					Point cellOriginPoint = this.GetCellOriginPoint(i, this.CellCol, new Size(this.CellW, this.CellH));
					double cellW = this.CellW;
					double cellH = this.CellH;
					base.get_Children().get_Item(i).Arrange(new Rect(cellOriginPoint.get_X() + (this.CellW - cellW) / 2.0, cellOriginPoint.get_Y() + (this.CellH - cellH) / 2.0, cellW, cellH));
				}
				else
				{
					base.get_Children().get_Item(i).Arrange(new Rect(0.0, 0.0, 0.0, 0.0));
				}
			}
			return new Size(this.CellW * (double)this.CellCol, this.CellH * (double)this.CellRow);
		}
		protected Point GetCellOriginPoint(int cellIndex, int cellCount, Size itemSize)
		{
			int num = (int)Math.Floor((double)(cellIndex / cellCount));
			int num2 = cellIndex - cellCount * num;
			Point result = new Point(itemSize.get_Width() * (double)num2, itemSize.get_Height() * (double)num);
			return result;
		}
		private static void OnItemPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == GridPanel.CellRowProperty)
			{
				(dependencyObject_0 as GridPanel).OnCellRowPropertyChanged(dependencyPropertyChangedEventArgs_0);
				(dependencyObject_0 as GridPanel).InvalidateMeasure();
				return;
			}
			if (dependencyPropertyChangedEventArgs_0.get_Property() == GridPanel.CellColProperty)
			{
				(dependencyObject_0 as GridPanel).OnCellColPropertyChanged(dependencyPropertyChangedEventArgs_0);
				(dependencyObject_0 as GridPanel).InvalidateMeasure();
			}
		}
		private void OnCellRowPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private void OnCellColPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
	}
}
